﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VipSys.DataLayer;
using VipSys.SystemEntity;
using VipSys.Common.Library;

namespace VipSys
{
    public partial class ExamPlaceMag : System.Web.UI.Page
    {
        private List<ExamPlace> m_lstExamPlace;
        protected void Page_Load(object sender, EventArgs e)
        {

            SysAccount sysAccount = Session["SysAccount"] as SysAccount;
            if (sysAccount == null)
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('登录超时，请重新登录!')</script>");
                return;
            }
            string action = Request["action"];
            if (!string.IsNullOrEmpty(action))
            {
                switch (action)
                {
                    case "BindGrid":
                        BindGrid();
                        return;
                    case "GetParentOrg":
                        GetParentOrg();
                        return;
                    case "BindPlaceType":
                        BindPlaceType();
                        return;
                    case "Add":
                        Add();
                        return;
                    case "Edit":
                        Edit();
                        return;
                    case "Del":
                        Del();
                        return;
                    default:
                        return;
                }
            }
        }

        private void Add()
        {
            string jsons = "";
            ExamPlace examPlace = new ExamPlace();
            var PlaceName = Request["PlaceName"];
            var ParentID = Request["ParentID"];
            var Description = Request["Description"];
            var PlaceType = Request["PlaceType"];
            examPlace.PlaceName = PlaceName;
            examPlace.ParentID = ParentID;
            examPlace.Description = Description;
            examPlace.PlaceType = PlaceType;
            short shRet = ExamPlaceAccess.Instance.Add(examPlace);
            if (shRet != SystemData.ReturnValue.OK)
            {
                jsons = "{\"success\":true,msg:\"新增失败\"}";
            }
            else
            {
                jsons = "{\"success\":true,msg:\"新增成功\"}";
            }
            this.Response.Clear();
            this.Response.Write(jsons);
            this.Response.End();
        }
 
        private void Edit()
        {
            string jsons = "";
            ExamPlace examPlace = new ExamPlace();
            var PlaceName = Request["PlaceName"];
            var ParentID = Request["ParentID"];
            var Description = Request["Description"];
            var PlaceType = Request["PlaceType"];
            var ID = Request["ID"];
            examPlace.ID = Int32.Parse(ID);
            examPlace.PlaceName = PlaceName;
            examPlace.ParentID = ParentID;
            examPlace.Description = Description;
            examPlace.PlaceType = PlaceType;
            short shRet = ExamPlaceAccess.Instance.Edit(examPlace);
            if (shRet != SystemData.ReturnValue.OK)
            {
                jsons = "{\"success\":true,msg:\"编辑失败\"}";
            }
            else
            {
                jsons = "{\"success\":true,msg:\"编辑成功\"}";
            }
            this.Response.Clear();
            this.Response.Write(jsons);
            this.Response.End();
        }
        private void Del()
        {
            string jsons = "";
            ExamPlace examPlace = new ExamPlace();
            var deletesplit = Request["deletesplit"];
            foreach (var ID in deletesplit.Split(','))
            {
                examPlace.ID = int.Parse(ID);
                short shRet = ExamPlaceAccess.Instance.Del(examPlace);
                if (shRet != SystemData.ReturnValue.OK)
                {
                    jsons = "{\"success\":true,msg:\"删除失败\"}";
                    break;
                }
            }
            if (string.IsNullOrEmpty(jsons))
                jsons = "{\"success\":true,msg:\"删除成功\"}";
            this.Response.Clear();
            this.Response.Write(jsons);
            this.Response.End();
        }
        private void BindGrid()
        {
            JSONHelper json = new JSONHelper();
            json.success = true;
            string jsons = "";
            ExamPlace query = new ExamPlace();
            query.Start = Request.Form["start"];
            query.Limit = Request.Form["limit"];
            string MenuTitle = Request.Form["MenuTitle"];
            if (query.Start != null && query.Limit != null)
            {
                json.success = true;
                short shRet = ExamPlaceAccess.Instance.GetExamPlacePageList(query, ref this.m_lstExamPlace);

                foreach (ExamPlace item in this.m_lstExamPlace)
                {
                    json.AddItem("ID", item.ID.ToString());
                    json.AddItem("PlaceName", item.PlaceName);
                    json.AddItem("ParentID", item.ParentID);
                    if (!string.IsNullOrEmpty(item.ParentID))
                    {
                        ExamPlace result = new ExamPlace();
                        result.ID = int.Parse(item.ParentID);
                        ExamPlaceAccess.Instance.GetExamPlace(result, ref result);
                        item.ParentName = result.PlaceName;
                    }
                    json.AddItem("ParentName", item.ParentName);
                    json.AddItem("Description", item.Description);
                    json.AddItem("PlaceType", item.PlaceType);
                    json.ItemOk();
                }
                int totalCount = 0;
                shRet = ExamPlaceAccess.Instance.GetTotalCount(query, ref totalCount);
                json.totlalCount = totalCount;
                if (json.totlalCount > 0)
                {
                    jsons = json.ToString();
                }
                else
                {
                    jsons = @"[]";
                }
            }
            else
            {
                jsons = @"{success:false}";
            }

            this.Response.Clear();
            this.Response.Write(jsons);
            this.Response.End();
        }
        private void GetParentOrg()
        {
            JSONHelper json = new JSONHelper();
            json.success = true;
            string jsons = "";
            ExamPlace query = new ExamPlace();

            query.Start = Request.Form["start"];
            query.Limit = Request.Form["limit"];
            string PlaceName = Request.Form["PlaceName"];
            string PlaceType = Request.Form["PlaceType"];
            query.PlaceName = PlaceName;
            query.PlaceType = PlaceType;
            if (query.Start != null && query.Limit != null)
            {
                json.success = true;
                short shRet = ExamPlaceAccess.Instance.GetExamPlacePageList(query, ref this.m_lstExamPlace);

                foreach (ExamPlace item in this.m_lstExamPlace)
                {
                    json.AddItem("ID", item.ID.ToString());
                    json.AddItem("PlaceName", item.PlaceName);
                    json.AddItem("PlaceType", item.PlaceType);
                    json.ItemOk();
                }
                int totalCount = 0;
                shRet = ExamPlaceAccess.Instance.GetTotalCount(query, ref totalCount);
                json.totlalCount = totalCount;
                if (json.totlalCount > 0)
                {
                    jsons = json.ToString();
                }
                else
                {
                    jsons = @"[]";
                }
            }
            else
            {
                jsons = @"{success:false}";
            }

            this.Response.Clear();
            this.Response.Write(jsons);
            this.Response.End();
        }
        public void BindPlaceType()
        {
            JSONHelper json = new JSONHelper();
            json.success = true;
            string jsons = "";
            json.success = true;
            List<SysCombo> lstRoleType = new List<SysCombo>();
            ComboAccess.Instance.GetPlaceTypes(ref lstRoleType);
            foreach (var row in lstRoleType)
            {
                json.AddItem("value", row.Value);
                json.AddItem("name", row.Name);
                json.ItemOk();
            }
            json.totlalCount = lstRoleType.Count();
            if (json.totlalCount > 0)
            {
                jsons = json.ToString();
            }
            else
            {
                jsons = @"[]";
            }
            this.Response.Clear();
            this.Response.Write(jsons);
            this.Response.End();
        }

    }
}